#include "StdAfx.h"
#include "InputManager.h"


InputManager::InputManager(void)
{
	curPos=lastPos=Vec3();
	rotSpeed=1;
	moveSpeed=0.3;
	panSpeed=0.01;
	hasMoved=false;
}
InputManager::~InputManager(void)
{
}
void InputManager::ProcessKeys(WPARAM key)
{
	Pawn* ghostie=_PAWN(_ACTOR("Ghostie"));
	float movAcc=1000;
	if(!ghostie)
		return;
	switch(key)
	{
	case VK_LEFT:
		ghostie->GetPhysics()->moveGlobalPosition(ghostie->GetPhysics()->getGlobalPosition()+Vec3(-1,0,0));
		break;
	case VK_RIGHT:
		ghostie->GetPhysics()->addForceAtLocalPos(Vec3(movAcc,0,0), Vec3(), NX_IMPULSE);
		break;
	case VK_UP:
		ghostie->GetPhysics()->addForceAtLocalPos(Vec3(0,0,-movAcc), Vec3(), NX_IMPULSE);
		break;
	case VK_DOWN:
		ghostie->GetPhysics()->addForceAtLocalPos(Vec3(0,0,-movAcc), Vec3(), NX_IMPULSE);
		break;
	default:
		break;
	}
}
void InputManager::ProcessChars(char key)
{
	float movAcc=5000;
	Vec3 tang=_MAIN_CAMERA->GetLookDir();
	Vec3 norm=_MAIN_CAMERA->GetStrafeDir();
	tang.y=norm.y=0;
	tang.normalize();
	norm.normalize();
	switch(key)
	{
	case '1':
		globals.Graphics.DisplayMap=globals.Graphics.DeferredMap;
		break;
	case '2':
		globals.Graphics.DisplayMap=globals.Graphics.DiffuseMap;
		break;
	case '3':
		globals.Graphics.DisplayMap=globals.Graphics.TotalLightMap[globals.Graphics.GetCurLightMapID()];
		break;
	case '4':
		globals.Graphics.DisplayMap=globals.Graphics.TotalSpecularMap[globals.Graphics.GetCurLightMapID()];
		break;
	case '5':
		globals.Graphics.DisplayMap=globals.Graphics.DepthMap;
		break;
	case '6':
		globals.Graphics.DisplayMap=globals.Graphics.LightPassMap;
		break;
	case '7':
		globals.Graphics.DisplayMap=globals.Graphics.DownsampleMap[3];
		break;
	case '8':
		globals.Graphics.DisplayMap=globals.Graphics.BlurredMap[1];
		break;
	case '0':
		globals.Graphics.DisplayMap=globals.Graphics.BloomedMap;
		break;
	case '-':
		globals.Graphics.BloomFactor-=5.0/60.0f;
		break;
	case '=':
		globals.Graphics.BloomFactor+=5.0/60.0f;
		break;
	case '[':
		globals.Graphics.LightCutoff=std::max(globals.Graphics.LightCutoff-1.0f/60.0f, 0.0f);
		break;
	case ']':
		globals.Graphics.LightCutoff=std::min(globals.Graphics.LightCutoff+1.0f/60.0f, 1.0f);
		break;
	case 'q':
		globals.Graphics.bClearSpecular=!globals.Graphics.bClearSpecular;
		break;
	case 'e':
		globals.Graphics.bUseLightPass=!globals.Graphics.bUseLightPass;
		break;
	case 'r':
		globals.Graphics.bDebugDraw=!globals.Graphics.bDebugDraw;
		break;
	case 't':
		globals.Graphics.bDoubleShadows=!globals.Graphics.bDoubleShadows;
		break;
	case ' ':
		globals.Game.bPaused=!globals.Game.bPaused;
		break;
	case ',':
		_PLAYER_MODEL->DiffuseFactor=std::max(_PLAYER_MODEL->DiffuseFactor-1.0f/60.0f, 0.0f);
		break;
	case '.':
		_PLAYER_MODEL->DiffuseFactor=std::min(_PLAYER_MODEL->DiffuseFactor+1.0f/60.0f, 1.0f);
		break;
	case '`':
		_PLAYER->Models[0]->bReflects=!_PLAYER->Models[0]->bReflects;
		_PLAYER->Models[0]->bRefracts=!_PLAYER->Models[0]->bRefracts;
		break;
	case 'w':
	case 'W':
		Move(tang*movAcc);
		break;
	case 's':
	case 'S':
		Move(tang*-movAcc);
		break;
	case 'a':
	case 'A':
		Move(norm*-movAcc);
		break;
	case 'd':
	case 'D':
		Move(norm*movAcc);
		break;
	default: 
		break;
	}
}
void InputManager::Move(Vec3 dir)
{
	Pawn* player=_PAWN(_ACTOR("PacMan"));
	if(player/*&&!hasMoved*/)
	{
		player->GetPhysics()->setLinearVelocity(dir/50.0);
		
		//player->GetPhysics()->addForceAtLocalPos(dir, Vec3(0,0,0), NX_IMPULSE);
		//player->GetPhysics()->setLinearDamping(0.0f);
		//player->GetPhysics()->setAngularDamping(0.0f);
		hasMoved=true;
	}
}
void InputManager::ProcessMousePress(unsigned int flags)
{
	nFlags=flags;
}
void InputManager::ProcessMouseMove(Vec3 newPos)
{
	lastPos=curPos;
	curPos=newPos;
	Vec3 dv=curPos-lastPos;
	sptr<Camera> camera=globals.Game.MainScene->MainCamera;
	if (nFlags & MK_LBUTTON)
	{
		Vec3 dv_temp(dv*-rotSpeed);
		camera->Rot+=Rotator(-dv.x,-dv.y);
	}
	// Right mouse button
	if (nFlags & MK_RBUTTON)
	{
		camera->OrbitRot+=Rotator(dv.x,dv.y);
	}
	// Middle mouse button
	if (nFlags & MK_MBUTTON)
	{
		camera->FocusDistance+=-dv.y*moveSpeed;
	}
}
